package com.jk.garden1.controller;

import com.jk.garden1.dao.SystemLogDao;
import com.jk.garden1.entity.StaffEntity;
import com.jk.garden1.entity.SystemLogEntity;
import com.jk.garden1.entity.UserMiniEntity;
import com.jk.garden1.entity.UserTableEntity;
import com.jk.garden1.service.LoginService;
import com.jk.garden1.service.UserService;
import com.jk.garden1.util.Md5UUIDSaltUtil;
import com.jk.garden1.util.MsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("login")
public class Login {

    @Autowired
    private LoginService loginService;
    @Autowired
    private UserService userService;

    @Autowired
    private SystemLogDao systemLogDao;


    /**
     * 手机验证码登录
     * @param userPhone
     * @return
     */
//    @RequestMapping("getPhoneLogin")
//    public Object getPhoneLogin(String userPhone){
//        return loginService.getPhoneLogin(userPhone);
//    }


    /**
     * 获取验证码
     * @param userPhone
     * @return
     */
    @RequestMapping("getCode")
    public Object getCode(String userPhone) throws Exception {
        return loginService.getCode(userPhone);
    }

    /**
     * 获取验证码
     * @param
     * @return
     */
    //登录接口
    @PostMapping("login")
    public Map login(@RequestParam("username")String username, @RequestParam("password")String password){
        UserTableEntity staff = userService.findByUsername(username);

        if (staff == null) {
            return MsgUtil.returnMsg(250,"账号错误",null);
        } else {
            String salt = staff.getSalt();
            String mySalt = salt.substring(0, 5);
            String md5Code = Md5UUIDSaltUtil.createMd5Code(password + mySalt);
            if (!staff.getPassword().equals(md5Code)) {
                return MsgUtil.returnMsg(251, "密码错误", null);
            } else if(staff.getStatus().equals("停用")){
                return MsgUtil.returnMsg(252, "账号已被停用，请联系管理员", null);
            }else {
                //通过UUID生成token字符串,并将其以string类型的数据保存在redis缓存中，key为token，value为username
                String token = UUID.randomUUID().toString().replaceAll("-", "");
                staff.setToken(token);
                staff.setPassword(password);

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date();
                String str = simpleDateFormat.format(date);

                SystemLogEntity systemLogEntity = new SystemLogEntity();
                systemLogEntity.setOnefunction("系统日志");
                systemLogEntity.setTwofunction("用户管理");
                systemLogEntity.setLogcontent("登录用户");
                systemLogEntity.setCreateperson(staff.getName());
                systemLogEntity.setCreatetime(str);
                systemLogDao.insert(systemLogEntity);


                return MsgUtil.returnMsg(200, "登陆成功", staff);
            }
        }
    }

    //注册验证码
    @RequestMapping("sendCode")
    public Map sendCode(String phone){
        return userService.SendCode(phone);
    }

    //注册账号
    @RequestMapping("loginadd")
    public Map registerUser(@RequestBody UserTableEntity usertableEntity){
        return userService.registerUser(usertableEntity);
    }


    /**
     * 获取验证码
     * @param
     * @return
     */
    //手机号登录接口
    @PostMapping("loginsjh")
    public Map loginsjh(@RequestParam("phone")String phone, @RequestParam("password")String password){
        UserTableEntity staff = userService.findByUser(phone);

        if (staff == null) {
            return MsgUtil.returnMsg(250,"账号错误",null);
        } else {
            String salt = staff.getSalt();
            String mySalt = salt.substring(0, 5)                         ;
            String md5Code = Md5UUIDSaltUtil.createMd5Code(password + mySalt);
            if (!staff.getPassword().equals(md5Code)) {
                return MsgUtil.returnMsg(251, "密码错误", null);
            } else {
                //通过UUID生成token字符串,并将其以string类型的数据保存在redis缓存中，key为token，value为username
                String token = UUID.randomUUID().toString().replaceAll("-", "");
                staff.setToken(token);
                staff.setPassword(password);
                return MsgUtil.returnMsg(200, "登陆成功", staff);
            }
        }
    }


    //查询
    @RequestMapping("selelist")
    public UserTableEntity selelist(Integer id){
       return userService.selelist(id);
    }

    //修改密码
    @RequestMapping("getpwdupdate")
    public void getpwdupdate(UserTableEntity userEntity){
        userService.getpwdupdate(userEntity);
    }

    /**
     * 验证登录
     * @param userMiniEntity
     * @return
     */
    @RequestMapping("loginCode")
    public Object loginCode(@RequestBody UserMiniEntity userMiniEntity){
        return userService.loginCode(userMiniEntity);
    }
}
